<template>
  <div class="page-container">
    <!-- 顶部导航 -->
    <header class="header">
      <div class="header-left">
        <div class="brand">
          <div class="logo-wrapper">
            <img src="../assets/logo.svg" alt="Logo" class="logo">
          </div>
          <h1 class="brand-name">协同空间</h1>
        </div>
        <div class="search-bar">
          <i class="search-icon">🔍</i>
          <input type="text" placeholder="搜索文档..." class="search-input">
        </div>
      </div>
      <div class="header-right">
        <button class="create-btn" @click="goToNewDocument">
          <span class="plus-icon">+</span>
          新建文档
        </button>
        <div class="user-info" ref="userMenuRef" @click.stop="toggleUserMenu">
          <div class="avatar">{{ userStore.userInfo?.username?.[0] || '用' }}</div>
          <div class="badge">3</div>
          
          <!-- 用户下拉菜单 -->
          <div v-show="showUserMenu" class="user-dropdown">
            <div class="dropdown-item">
              <i class="fas fa-user"></i>
              个人中心
            </div>
            <div class="dropdown-item">
              <i class="fas fa-cog"></i>
              设置
            </div>
            <div class="dropdown-item logout" @click="handleLogout">
              <i class="fas fa-sign-out-alt"></i>
              退出登录
            </div>
          </div>
        </div>
      </div>
    </header>

    <!-- 主要内容区 -->
    <main class="main-content">
      <!-- 左侧菜单 -->
      <nav class="side-menu">
        <div class="menu-group">
          <a href="#" class="menu-item active">
            <span class="menu-icon">📄</span>
            <span class="menu-text">所有文档</span>
            <span class="menu-count">12</span>
          </a>
          <a href="#" class="menu-item">
            <span class="menu-icon">⭐</span>
            <span class="menu-text">收藏夹</span>
            <span class="menu-count">5</span>
          </a>
          <a href="#" class="menu-item">
            <span class="menu-icon">👥</span>
            <span class="menu-text">共享文件</span>
            <span class="menu-count">8</span>
          </a>
          <a href="#" class="menu-item">
            <span class="menu-icon">🗑️</span>
            <span class="menu-text">回收站</span>
          </a>
        </div>
      </nav>

      <!-- 文档列表区域 -->
      <div class="content-area">
        <div class="document-grid">
          <!-- 项目计划书 -->
          <div class="document-card">
            <div class="card-header">
              <div class="doc-type">
                <span class="doc-icon">📄</span>
                <span class="doc-label">文档</span>
              </div>
              <div class="card-actions">
                <div class="collaborators">
                  <span class="collab-icon">👥</span>
                  <span class="collab-count">3</span>
                </div>
                <span class="star-icon">⭐</span>
              </div>
            </div>
            <h3 class="doc-title">项目计划书</h3>
            <p class="update-time">更新于 2小时前</p>
          </div>

          <!-- 数据分析报告 -->
          <div class="document-card">
            <div class="card-header">
              <div class="doc-type">
                <span class="doc-icon">📊</span>
                <span class="doc-label">表格</span>
              </div>
              <div class="card-actions">
                <div class="collaborators">
                  <span class="collab-icon">👥</span>
                  <span class="collab-count">5</span>
                </div>
                <span class="star-icon">⭐</span>
              </div>
            </div>
            <h3 class="doc-title">数据分析报告</h3>
            <p class="update-time">更新于 昨天</p>
          </div>

          <!-- 会议记录 -->
          <div class="document-card">
            <div class="card-header">
              <div class="doc-type">
                <span class="doc-icon">📝</span>
                <span class="doc-label">笔记</span>
              </div>
              <div class="card-actions">
                <div class="collaborators">
                  <span class="collab-icon">👥</span>
                  <span class="collab-count">2</span>
                </div>
              </div>
            </div>
            <h3 class="doc-title">会议记录</h3>
            <p class="update-time">更新于 3天前</p>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const showUserMenu = ref(false)
const userMenuRef = ref<HTMLElement | null>(null)

const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value
}

const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}

const goToNewDocument = () => {
  router.push('/about')
}

// 处理点击事件
const handleClickOutside = (event: MouseEvent) => {
  if (userMenuRef.value && !userMenuRef.value.contains(event.target as Node)) {
    showUserMenu.value = false
  }
}

// 组件挂载时添加事件监听
onMounted(() => {
  document.addEventListener('click', handleClickOutside)
})

// 组件卸载时移除事件监听
onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>

<style scoped>
.page-container {
  min-height: 100vh;
  background-color: #f8fafc;
  width: 100vw;
  overflow-x: hidden;
}

/* 顶部导航样式 */
.header {
  height: 72px;
  background-color: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 32px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.02);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 48px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.logo-wrapper {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.2);
  transition: all 0.3s ease;
}

.logo-wrapper:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(99, 102, 241, 0.3);
}

.logo {
  width: 100%;
  height: 100%;
  filter: brightness(0) invert(1);
}

.brand-name {
  font-size: 20px;
  font-weight: 600;
  color: #1e293b;
  letter-spacing: -0.02em;
}

.search-bar {
  position: relative;
  width: 420px;
  left: 20%;
}

.search-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
  font-size: 16px;
}

.search-input {
  width: 100%;
  height: 44px;
  padding: 0 16px 0 44px;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  font-size: 15px;
  background-color: #ffffff;
  transition: all 0.3s ease;
}

.search-input:hover {
  border-color: #cbd5e1;
  background-color: #ffffff;
}

.search-input:focus {
  outline: none;
  border-color: #6366f1;
  background-color: #fff;
  box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.create-btn {
  height: 44px;
  padding: 0 24px;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.2);
}

.create-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(99, 102, 241, 0.3);
}

.plus-icon {
  font-size: 18px;
  font-weight: bold;
}

.user-info {
  position: relative;
  cursor: pointer;
}

.avatar {
  width: 44px;
  height: 44px;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  color: white;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.2);
}

.avatar:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(99, 102, 241, 0.3);
}

.badge {
  position: absolute;
  top: -6px;
  right: -6px;
  background-color: #ef4444;
  color: white;
  font-size: 12px;
  padding: 0 6px;
  height: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  box-shadow: 0 4px 8px rgba(239, 68, 68, 0.2);
}

.user-dropdown {
  position: absolute;
  right: 0;
  top: 100%;
  margin-top: 8px;
  width: 192px;
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  padding: 8px 0;
  z-index: 100;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  color: #374151;
  transition: all 0.2s ease;
}

.dropdown-item:hover {
  background-color: #f3f4f6;
}

.dropdown-item i {
  width: 16px;
  text-align: center;
}

.dropdown-item.logout {
  color: #ef4444;
}

/* 主要内容区样式 */
.main-content {
  display: flex;
  padding-top: 72px;
  min-height: calc(100vh - 72px);
}

/* 侧边菜单样式 */
.side-menu {
  width: 260px;
  background-color: #fff;
  border-right: 1px solid rgba(0, 0, 0, 0.06);
  padding: 24px 0;
  height: calc(100vh - 72px);
  position: fixed;
  left: 0;
}

.menu-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 0 12px;
}

.menu-item {
  height: 48px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 14px;
  color: #475569;
  text-decoration: none;
  font-size: 15px;
  border-radius: 12px;
  transition: all 0.3s ease;
  position: relative;
}

.menu-item:hover {
  background-color: #f8fafc;
  color: #1e293b;
}

.menu-item.active {
  background-color: #eff6ff;
  color: #3b82f6;
  font-weight: 500;
}

.menu-icon {
  font-size: 20px;
  opacity: 0.9;
}

.menu-text {
  flex: 1;
}

.menu-count {
  font-size: 13px;
  color: #94a3b8;
  background-color: #f1f5f9;
  padding: 3px 10px;
  border-radius: 14px;
  font-weight: 500;
}

.menu-item.active .menu-count {
  background-color: #dbeafe;
  color: #3b82f6;
}

/* 文档列表区域样式 */
.content-area {
  flex: 1;
  margin-left: 260px;
  padding: 32px;
}

.document-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 28px;
  padding: 8px;
}

.document-card {
  background-color: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 16px;
  padding: 24px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.document-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.document-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.08);
  border-color: rgba(99, 102, 241, 0.2);
}

.document-card:hover::before {
  opacity: 1;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.doc-type {
  display: flex;
  align-items: center;
  gap: 10px;
}

.doc-icon {
  font-size: 28px;
}

.doc-label {
  font-size: 13px;
  color: #64748b;
  background-color: #f8fafc;
  padding: 4px 10px;
  border-radius: 8px;
  font-weight: 500;
}

.card-actions {
  display: flex;
  align-items: center;
  gap: 14px;
}

.collaborators {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #64748b;
  font-size: 13px;
  background-color: #f8fafc;
  padding: 4px 10px;
  border-radius: 8px;
  font-weight: 500;
}

.collab-icon {
  font-size: 16px;
}

.star-icon {
  color: #eab308;
  font-size: 18px;
  transition: transform 0.3s ease;
}

.star-icon:hover {
  transform: scale(1.2);
}

.doc-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 10px;
  line-height: 1.5;
}

.update-time {
  margin: 0;
  font-size: 13px;
  color: #64748b;
  display: flex;
  align-items: center;
  gap: 6px;
}

/* 响应式设计 */
@media (max-width: 1280px) {
  .document-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 24px;
  }
  
  .search-bar {
    width: 360px;
  }
}

@media (max-width: 1024px) {
  .search-bar {
    width: 300px;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 0 20px;
  }
  
  .side-menu {
    display: none;
  }
  
  .content-area {
    margin-left: 0;
    padding: 20px;
  }
  
  .search-bar {
    display: none;
  }
  
  .document-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .create-btn {
    padding: 0 20px;
  }
}
</style>